package spring.cloud.tasks.tasks_assign_manager.task_item_assign_event_handler;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.TaskEnableOrDisEnableTaskItemAssignEvent;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.TaskItemAssignEvent;

@Slf4j
@Setter
@Getter
public class TaskEnableOrDisEnableTaskItemAssignEventHandler extends TaskItemAssignEventHandler {


    private TaskItemAssignService taskItemAssignService;


    public TaskEnableOrDisEnableTaskItemAssignEventHandler(TaskItemAssignService taskItemAssignService) {
        this.taskItemAssignService = taskItemAssignService;

    }

    @Override
    public void handle(TaskItemAssignEvent taskItemAssignEvent) throws Exception {
        try {
            if (taskItemAssignEvent instanceof TaskEnableOrDisEnableTaskItemAssignEvent) {
                TaskEnableOrDisEnableTaskItemAssignEvent event = (TaskEnableOrDisEnableTaskItemAssignEvent) taskItemAssignEvent;
                if (TaskEnableOrDisEnableTaskItemAssignEvent.type_task_enable.equals(event.getType())) {
                    taskItemAssignService.taskEnableAllotTask(event.getTaskId());
                } else if (TaskEnableOrDisEnableTaskItemAssignEvent.type_task_disable.equals(event.getType())) {
                    taskItemAssignService.taskDisableAllotTask(event.getTaskId());
                } else if (TaskEnableOrDisEnableTaskItemAssignEvent.type_task_force_reallot.equals(event.getType())) {
                    taskItemAssignService.taskExecuteForceTaskItemAllotTask(event.getTaskId());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
